<?php


class MyPDO
{
    //成员变量
    private $mysql;
    private $db="test123";
    private $host="127.0.0.1";
    private $user="root";
    private $pass="";
    private $charts="utf8";

    //构造方法
    function  __construct()
    {
        try {
            //1.连接数据库
            $this->mysql = new PDO("mysql:dbname=$this->db;host=$this->host", "$this->user", "$this->pass");
            $this->mysql->exec("SET NAMES '$this->charts';");
        }
        catch(Exception $e){
            echo "连接失败".$e->getMessage();
            var_dump($e);
            die();
        }

    }
    //增
    function insert($sql){
        return $this->mysql->exec($sql);
    }
    //删
    function delete($sql){
        return $this->mysql->exec($sql);
    }
    //改
    function update($sql){
        return $this->mysql->exec($sql);
    }
    //查
    function select($sql){
        $res=$this->mysql->query($sql,PDO::FETCH_ASSOC);
        //希望存储的是普通关联数组
        $arr=[];
        foreach ($res as $row)
        {
            $arr[]=$row;
        }
        return $arr;
    }

}